<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.keyTask_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
       <template slot-scope="scope" slot="menu">
        <el-button
          type="text"
          icon="el-icon-circle-plus-outline"
          size="small"
          @click.stop="handlepatrol(scope.row)"
        >巡查
        </el-button>
       </template>
    
    </avue-crud>
        <!-- 巡查 -->
    <el-dialog title="巡查"
                   append-to-body
                   :visible.sync="patrolBoxShow"
                   width="50%">
    <el-form ref="PatrolForm" :rules="rules" :model="patrolForm" label-width="180px" size="mini">
     <el-card shadow="hover">
        <el-row>
          <el-col :span="24" v-show="false">
            <el-form-item label="id：" >
               <el-input :disabled="true" name="taskId"   v-model="patrolForm.taskId"/>
            </el-form-item>
          </el-col>
           <el-col :span="24">
            <el-form-item label="任务：" >
               <el-input :disabled="true" name="name"   v-model="patrolForm.name"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="状态：" prop= "status">
               <el-radio-group name="status" v-model="patrolForm.status">
                <el-radio :label="1">正常</el-radio>
                <el-radio :label="2">异常</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
           <el-col :span="24" >
            <el-form-item label="执行情况：" prop= "conditions">
               <el-input type="textarea"  name="conditions"   v-model="patrolForm.conditions"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="文件上传：">
              <el-upload
                    class="upload-demo"
                    drag
                    action="https://jsonplaceholder.typicode.com/posts/"
                    multiple>
                    <i class="el-icon-upload"></i>
                    <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                  </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
              <el-form-item class="align-text:center">
                <el-button type="primary" @click="handlePatrolSave('PatrolForm')">保存</el-button>
                <el-button @click="handleCancel('PatrolForm')">关闭</el-button>
              </el-form-item>
         </el-row>
     </el-card>
    </el-form>
    </el-dialog>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove,patrolSave} from "@/api/xapply/keyplace/keyTask";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {},
        patrolForm: {},
        query: {},
        patrolBoxShow: false,
        //Form表单校验
        rules: {
          conditions: [
            { required: true, message: '请输入执行情况', trigger: 'blur' }
          ],
          status: [
            {required: true, message: '请选择状态', trigger: 'blur' }
          ],
        },
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 30,
          dialogWidth: 850,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          excelBtn: true,
          dialogClickModal: false,
          column: [
            {
              label: "主键id",
              prop: "id",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
             {
              label: "任务名称",
              prop: "name",
              type: "input",
              span: 24,
              rules: [{
                required: true,
                message: "请输入任务名称",
                trigger: "blur"
              }],
            },
            {
              label: "网格员",
              prop: "gridId",
              type: "select",
              span: 24,
              dicUrl: "/api/smart-user/selectGridWorker",
              props: {
                label: "title",
                value: "value"
              },
              rules: [{
                required: true,
                message: "请输入网格员",
                trigger: "blur"
              }],
            },
             {
              label: "场所",
              prop: "keyPlace",
              type: "select",
              span: 24,
              search: true,
              dicUrl: "/api/smart-xapply/keyplace/keyPlace/getList",
              props: {
                label: "title",
                value: "value"
              },
              rules: [{
                required: true,
                message: "请输入场所",
                trigger: "blur"
              }],
            },
            {
              label: "天数",
              prop: "day",
              type: "input",
              span: 24,
              hide: true,
              rules: [{
                required: true,
                message: "请输入天数",
                trigger: "blur"
              }],
            },
            {
              label: "次数",
              prop: "time",
              type: "input",
              span: 24,
              hide: true,
              rules: [{
                required: true,
                message: "请输入次数",
                trigger: "blur"
              }],
            },
           
            {
              label: "是否循环",
              prop: "sfxh",
              type: "radio",
              span: 24,
              hide: true,
              dicData: [
                  {
                    label: "循环",
                    value: "0"
                  },
                  {
                    label: "不循环",
                    value:"1"
                  }
              ],
              rules: [{
                required: true,
                message: "请输入是否循环",
                trigger: "blur"
              }],
            },
             {
              label: "频次",
              prop: "frequency",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: false,
            },
             {
              label: "进度",
              prop: "speed",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: false,
            },
             {
              label: "事件",
              prop: "event",
              type: "textarea",
              span: 24,
              overHidden: true,
              rules: [{
                required: true,
                message: "请输入事件",
                trigger: "blur"
              }],
            },
            {
              label: "已经巡查次数",
              prop: "doTime",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
            {
              label: "任务开始时间",
              prop: "beginTime",
              type: "input",
              span: 24,
              hide: true,
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              rules: [{
                required: true,
                message: "请输入任务开始时间",
                trigger: "blur"
              }],
            },
            {
              label: "任务结束时间",
              prop: "endTime",
              type: "input",
              span: 24,
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              rules: [{
                required: true,
                message: "请输入任务结束时间",
                trigger: "blur"
              }],
            },
          ]
        },
        
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.keyTask_add, false),
          viewBtn: this.vaildData(this.permission.keyTask_view, false),
          delBtn: this.vaildData(this.permission.keyTask_delete, false),
          editBtn: this.vaildData(this.permission.keyTask_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      handlepatrol(row){
        console.log(row.id);
        this.patrolForm.taskId= row.id;
        this.patrolForm.name = row.name;
        this.patrolBoxShow = true
      },
      handlePatrolSave(FormName,loading){
        let form = null;
        form = this.patrolForm;
        const params = form;
        patrolSave(params).then((res) => {
          this.$message({
            type: "success",
            message: "保存!"
          });
          window.console.log(res);
          this.patrolBoxShow = false;
          this.$refs[FormName].resetFields();
          this.onLoad(this.page);
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      handleCancel(FormName){
        this.patrolBoxShow = false;
        this.$refs[FormName].resetFields();
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
